package ustc.xiaoyang.karl.linkedlist;

/**
 * 203.移除链表元素
 */
public class RemoveLinkedListElement203 {
    public static void main(String[] args) {

        ListNode five = new ListNode(1);
        ListNode four = new ListNode(2,five);
        ListNode three = new ListNode(3,four);
        ListNode two = new ListNode(4,three);
        ListNode one = new ListNode(5,two);
        RemoveLinkedListElement203 r = new RemoveLinkedListElement203();
        ListNode listNode = r.removeElements(one, 4);
        r.printList(listNode);
    }
    public ListNode removeElements(ListNode head, int val) {
       ListNode dummy = new ListNode(0,head);
       ListNode cur = dummy;
       while(cur.next != null){
           if(cur.next.val == val){
               cur.next = cur.next.next;
           }else{
               cur = cur.next;
           }

       }
       return dummy.next;
    }

    public void printList(ListNode head){
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
